﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using HalconDotNet;

namespace halconScanCode
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
        FilterInfoCollection videoDevices;//摄像头设备集合
        VideoCaptureDevice videoSource;//捕获设备源
        Bitmap img;//处理图片
        HDataCode2D hDataCode2D = new HDataCode2D();

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //先检测电脑所有的摄像头
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo videoDevice in videoDevices)
            {
                comboBox1.Items.Add(videoDevice.Name);
            }

            hDataCode2D.CreateDataCode2dModel("QR Code", "default_parameters", "maximum_recognition");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (videoSource != null)
            {//在2个或以上摄像头进行切换时执行
                ShutCamera();
            }
            videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();

            button1.Visible = true;//开启“拍摄功能”
        }

        // 关闭并释放摄像头
        public void ShutCamera()
        {
            if (videoSourcePlayer1.VideoSource != null)
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
                videoSourcePlayer1.VideoSource = null;
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            ShutCamera();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
            //img = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            //pictureBox1.Image = img;
            // button2.Visible = true;//开启“保存”功能
        }

        private void ScanCode(HImage hImage)
        {
            try
            {
                //hWindowControl1.HalconWindow.ClearWindow();
                hDataCode2D.SetDataCode2dParam("model_type", 2);
                hDataCode2D.SetDataCode2dParam("mirrored", "no");
                hDataCode2D.SetDataCode2dParam("timeout", 200);
                hDataCode2D.SetDataCode2dParam("polarity", "dark_on_light");
                hDataCode2D.SetDataCode2dParam("module_size_min", 1);
                hDataCode2D.SetDataCode2dParam("module_size_max", 30);
                //HXLDCont hXLDCont = hImage.FindDataCode2d(hDataCode2D, "stop_after_result_num", 6, out HTuple resultHandles, out HTuple decodedDataStrings);
                hWindowControl1.HalconWindow.SetLineWidth(10);
                hWindowControl1.HalconWindow.SetColor("green");
                hWindowControl1.HalconWindow.DispImage(hImage);
                //hWindowControl1.HalconWindow.DispObj(hXLDCont);
            }
            catch (Exception)
            {

                
            }
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            img = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄
            HObject hObject = BitmapToHobject(img);
            ScanCode(new HImage(hObject));
        }


        public HObject BitmapToHobject(Bitmap bitmap)
        {
            HObject hObject = new HObject();
            hObject.GenEmptyObj();
            Rectangle rectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bitmapData=bitmap.LockBits(rectangle, ImageLockMode.ReadWrite, bitmap.PixelFormat);
            int byteWidthg = bitmapData.Stride / 3;
            byte[] imageR = new byte[byteWidthg * bitmapData.Height];
            byte[] imageG = new byte[byteWidthg * bitmapData.Height];
            byte[] imageB = new byte[byteWidthg * bitmapData.Height];
            unsafe
            {
                byte* ptr = (byte*)bitmapData.Scan0;
                for (int i =0  ; i < bitmapData.Height; i++)
                {
                    for (int j = 0 ; j < bitmapData.Width; j++)
                    {
                        imageB[i * byteWidthg + j] = *(ptr + 0);
                        imageG[i * byteWidthg + j] = *(ptr + 1);
                        imageR[i * byteWidthg + j] = *(ptr + 2);
                        ptr += 3;
                    }
                    ptr += bitmapData.Stride - bitmapData.Width *3 ;
                }
                fixed (byte* pR= imageR,pG= imageG,pB= imageB)
                {
                    HOperatorSet.GenImage3(out hObject, "byte", bitmapData.Width, bitmapData.Height, new IntPtr(pR), new IntPtr(pG), new IntPtr(pB));
                }
            }
            bitmap.UnlockBits(bitmapData);
            return hObject;
        }



        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer1.Stop();
        }
    }
}
